﻿using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections;

public class PerfectPixelMonitor : Editor 
{
	[MenuItem ("Tools/PerfectPixel/Show Not PerfectPixel Nodes")]  
	public static void ShowNotPerfectPixelNormalNodes()
	{
		char[] trimEnd = (".prefab").ToCharArray();
		Debug.Log("开始检测Not PerfectPixel Label:......");
		
		string fileFolder = "Assets/GameResources/UI/Dep/";
	 	string[] files = Directory.GetFiles(fileFolder, "*", SearchOption.AllDirectories);
        foreach(string file in files)
        {			
			if(file.Contains(".meta"))
				continue;
			
			Object obj = AssetDatabase.LoadMainAssetAtPath(file);
			
			if(obj is GameObject)
			{
				GameObject gameObj = obj as GameObject;
				ShowNotPerfectPixelNormalNode(file.TrimEnd(trimEnd), gameObj.transform);
			}
		}
		
		Debug.Log("检测完毕！");
	}
	
	/// <summary>
	/// 检测非完美像素节点
	/// objPath:引用者路径
	/// trans:引用者
	/// </summary>
	static void ShowNotPerfectPixelNormalNode(string objPath, Transform trans)
	{		
		if(LayerMask.LayerToName(trans.gameObject.layer) != "U2D")
			return;
		
		bool checkSize = true;
		bool checkPos = true;
		
		if(trans.parent != null)
		{
			if(trans.parent.GetComponent<UITable>() != null)
				checkPos = false;
			if(trans.parent.GetComponent<UIGrid>() != null)
				checkPos = false;
//			if(trans.parent.GetComponent<UIMix>() != null)
//				checkPos = false;
		}
		
		if(trans.GetComponent<UIAnchor>() != null)
		{
			checkSize = false;
			checkPos = false;
		}
		
		if(trans.GetComponent<UIWidget>() != null)
			checkPos = false;
			
		bool isPerfectNode = true;
		if(checkSize)
		{
			Vector3 size = trans.localScale;
			if(!Mathf.Approximately(size.x, Mathf.RoundToInt(size.x)) || !Mathf.Approximately(size.y, Mathf.RoundToInt(size.y)))
				isPerfectNode = false;
			
			UISprite sprite = trans.GetComponent<UISprite>();
			if(sprite != null)
			{
				if(!(sprite is UISlicedSprite))
				{
//					if(sprite.size != sprite.spriteSize)
//						isPerfectNode = false;
				}
			}
			
			UILabel label = trans.GetComponent<UILabel>();
			if(label != null)
			{
				UIFont font = label.font;
				if(font != null)
				{
					float pixelSize = (font.atlas != null) ? font.atlas.pixelSize : 1f;
	
					Vector3 scale = label.cachedTransform.localScale;
					scale.x = font.size * pixelSize;
					scale.y = scale.x;
		
					if(label.cachedTransform.localScale != scale)
						isPerfectNode = false;
				}
			}
		}
				
		if(checkPos)
		{					
			Vector3 pos = trans.localPosition;
			if(!Mathf.Approximately(pos.x, Mathf.RoundToInt(pos.x)) || !Mathf.Approximately(pos.y, Mathf.RoundToInt(pos.y)))
				isPerfectNode = false;			
		}
		
		if(!isPerfectNode)
			Debug.Log(string.Format("Not PerfectPixel Node：{0}", objPath));	
				
		//子节点递归
		for(int j = 0; j < trans.childCount; j++)
		{
			Transform child = trans.GetChild(j);
			ShowNotPerfectPixelNormalNode(objPath + "/" + child.name, child);
		}
	}
	
	[MenuItem ("Tools/PerfectPixel/Make Node PerfectPixel")]  
	public static void MakeNodePerfectPixel()
	{
		if(Selection.activeTransform == null)
		{
			Debug.LogError("你还未选择节点！");
			return;
		}
		
		foreach (Transform trans in Selection.transforms) 
			NGUITools.MakePixelPerfect(trans, false);
	}
	
	[MenuItem ("Tools/PerfectPixel/Make Node And Children PerfectPixel")]  
	public static void MakeNodeAndChildrenPerfectPixel()
	{
		if(Selection.activeTransform == null)
		{
			Debug.LogError("你还未选择节点！");
			return;
		}
		
		foreach (Transform trans in Selection.transforms) 
			NGUITools.MakePixelPerfect(trans, true);
	}
}
